﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShopManager : MonoBehaviour {

    private PlayerManager m_playerManager;
    private MapManager m_mapManager;

    private GameObject m_buyButton;
    private GameObject m_shopPlayButton;

    private Vector3 point;
    private Quaternion cubeRotation;
    private Vector3 deltaUp;
    private Vector3 deltaDown;

    private bool canMove = false;
    public int activeCube = 0;
    
    public List<Transform> cubeList = new List<Transform>();
    public List<Material> materialList = new List<Material>();

    private Vector2 touchOrigin = -Vector2.one;

    void Awake () {
        m_playerManager = GameObject.Find("Player").GetComponent<PlayerManager>();
        m_mapManager = GameObject.Find("MapManager").GetComponent<MapManager>();
        m_shopPlayButton = GameObject.Find("Shop_Play_Button");
        m_buyButton = GameObject.Find("Shop_Buy_Button");
        activeCube = m_playerManager.GetCurrentCube();
    }

    private void Start()
    {
        point = m_mapManager.maplist[11][1].transform.position + Vector3.up * 0.254f / 2;
        cubeRotation = Quaternion.Euler(new Vector3(0, -45, 0));
        deltaUp = m_mapManager.maplist[11][1].transform.position - m_mapManager.maplist[9][0].transform.position;
        deltaDown = m_mapManager.maplist[7][3].transform.position - m_mapManager.maplist[11][1].transform.position;

        //初始化cube材质
        for(int i = 1; i < cubeList.Count; i++)
        {
            if (!m_playerManager.HaveCube(i))
            {
                cubeList[i].GetComponent<MeshRenderer>().material = materialList[materialList.Count - 1];
            }
        }
    }

    private void Update()
    {
        if (canMove)
        {
            MoveCubes(activeCube);
            if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
            {
                if (activeCube > 0)
                {
                    activeCube--;
                    ChangeUI();
                }
            }
            if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
            {
                if (activeCube < 20)
                {
                    activeCube++;
                    ChangeUI();
                }
            }

#if UNITY_IOS || UNITY_ANDROID
            if (Input.touchCount > 0)
            {
                Touch myTouch = Input.touches[0];

                if (myTouch.phase == TouchPhase.Began)
                    touchOrigin = myTouch.position;
                else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
                {
                    Vector2 touchEnd = myTouch.position;
                    float x = touchEnd.x - touchOrigin.x;
                    touchOrigin.x = -1;
                    if (activeCube > 0 && activeCube < 20)
                    {
                        activeCube += (int)x;
                    }
                }
            }
#endif
        }
        
    }

    public void EnterShop()
    {
        //更改地图
        for (int i = 0; i < 3; i++)
        {
            ChangeMap((i + 3) * 2, 0);
            ChangeMap((i + 8) * 2, 5);
        }
        m_mapManager.maplist[7][2].GetComponent<MeshRenderer>().material.color = new Color(125 / 255f, 169 / 255f, 233 / 255f);
        m_mapManager.maplist[7][2].GetComponent<Transform>().Find("normal_a2").GetComponent<MeshRenderer>().material.color = new Color(125 / 255f, 169 / 255f, 233 / 255f);

        //删除角色
        m_playerManager.DestoryCube();
        //放置cube
        SetCubePos();



        canMove = true;

        ChangeUI();

    }

    public void ExitShop()
    {
        canMove = false;
        for(int i = 0; i < cubeList.Count; i++)
        {
            cubeList[i].position = Vector3.back * 2;
        }
        m_playerManager.ChangeCube(activeCube);
    }

    private void ChangeUI()
    {
        if (m_playerManager.HaveCube(activeCube))
        {
            m_shopPlayButton.SetActive(true);
            m_buyButton.SetActive(false);
        }
        else
        {
            m_shopPlayButton.SetActive(false);
            m_buyButton.SetActive(true);
        }
    }

    private void ChangeMap(int i,int j)
    {
        GameObject go = m_mapManager.maplist[i][j];
        Vector3 pos = go.transform.position;
        Quaternion rot = go.transform.rotation;

        Destroy(go.gameObject);

        go = m_mapManager.maplist[i][0];
        go = Instantiate(Resources.Load("tile_white") as GameObject, pos, rot);

        Color color = new Color(124 / 255f, 155 / 255f, 230 / 255f);
        go.GetComponent<MeshRenderer>().material.color = color;
        go.GetComponent<Transform>().Find("normal_a2").GetComponent<MeshRenderer>().material.color = color;
        go.transform.SetParent(gameObject.transform);
    }

    public void Buy()
    {
        m_shopPlayButton.SetActive(true);
        m_buyButton.SetActive(false);
        cubeList[activeCube].GetComponent<MeshRenderer>().material = materialList[activeCube];
        m_playerManager.BuyCube(activeCube);
    }

    private void SetCubePos()
    {
        for(int i = 0; i < cubeList.Count; i++)
        {
            cubeList[i].rotation = cubeRotation;
            cubeList[i].position = point + i * deltaUp;
        }
    }

    private void MoveCubes(int cubeNum)
    {
        for(int i = 0; i < cubeList.Count; i++)
        {
            cubeList[i].position = Vector3.Lerp(cubeList[i].position, point + (i - cubeNum) * deltaUp, Time.deltaTime);
            if (i == cubeNum && (cubeList[i].position - point).sqrMagnitude<deltaUp.sqrMagnitude)
            {
                cubeList[i].position = Vector3.Lerp(cubeList[i].position, point + deltaDown, Time.deltaTime);
            }
        }
    }
}
